function B=combineEDFMAT_2014(fname)
%no help
if nargin==0
    [fname, fdir]=uigetfile('*.edf','Open an EDF file or DIE!');
    drawnow
    if fname==0
        help(mfilename)
        return
    end
    cd(fdir)
    if exist([fname(1:end-3) 'mat'],'file')~=2
        error('matching MAT file (%s) is missing',[fname(1:end-3) 'mat']);
    end
    if exist([fname(1:end-4) 'edfB.mat'],'file')==2
        reply=questdlg(sprintf('file:  %sedfB.mat exists!\n OVERWRITE???',fname(1:end-4)),'becareful!!!','yes','no','no');
        drawnow
        if ~strcmp(reply,'yes')
            disp(' ->overwrite canceled.')
            return
        end
    end
end
%% load EDF
tic
E=el2mat_b2(fname);
fprintf('loading %s took: %4.2f seconds\n',fname,toc)

% adjust EDF
eye.t=E.sampledata(:,1);
eye.xo=E.sampledata(:,2);eye.xo(abs(eye.xo)>10000000)=nan;eye.xo=(eye.xo-(E.res.pix_wide/2));
eye.yo=E.sampledata(:,3);eye.yo(abs(eye.yo)>10000000)=nan;eye.yo=(eye.yo-(E.res.pix_high/2));
eye.ao=E.sampledata(:,4);
int=1000/E.res.rec_rate; % force A & E to resolution of E recording
E.mestimes=floor(E.mestimes/int)*int;
E.mesindex=ones(size(E.mestimes));
mt=E.mestimes(E.mestimes>0);
%avoids 5 in-a-row of same times (otherwise have to go through times 1 by 1)
for ii =1:5
    [time, ind1{ii}]=intersect(eye.t,mt(ii:5:end)); %#ok<AGROW,ASGLU>
end
ind=sort(vertcat(ind1{:}));
if length(ind)<length(mt)
    ind=zeros(size(mt));
    warning('length(ind)<length(mt): not ok! we have to go through times 1 by 1') %#ok<WNTAG>
    for ii =1:length(mt)
        if find(eye.t==mt(ii))
            ind(ii)=find(eye.t==mt(ii));
        end
    end
end
E.mesindex(E.mestimes>0)=ind; % to find eyetrace easier
clear mt time ind1 ind ii

manual_override=false;
SEND_SCREEN_METRICS=E.messages(findincell(E.messages,'disttoscreen'));
if isempty(SEND_SCREEN_METRICS)
    manual_override=true;
else
    SEND_SCREEN_METRICS=SEND_SCREEN_METRICS{1};
    fred=textscan(SEND_SCREEN_METRICS,repmat('%s %s ',1,(length(strfind(SEND_SCREEN_METRICS,' '))+1)/2));
    for i = 1:2:length(fred)
        eval(['E.res.' lower(char(fred{i})) '=' char(fred{i+1}) ';'])
    end
    if ~isfield(E.res,'swidthinches') || ~isfield(E.res,'disttoscreen')
        manual_override=true;
    end
end
if manual_override
    E.res.manual=true;
    E.res.swidthinches=13;
    E.res.disttoscreen=23;
    disp('couldn''t find SEND_SCREEN_METRICS, using default 13inch screen viewed from 23inches away')
end

dphs=atan((E.res.swidthinches/2)/E.res.disttoscreen)*180/pi;% degrees per half of a 13inch screen from 23 inches away
pixperdeg=(E.res.pix_wide/2)/dphs; % pixels per degree
E.res.ppd2=pixperdeg;
eye.xo=eye.xo/pixperdeg;
eye.yo=eye.yo/pixperdeg;


%%
tic;
[eye.x, eye.y, eye.a, eye.blink]=EL1k_blinkremover_2013(eye.t, eye.xo, eye.yo, eye.ao,3);
eye.speed=xyt2speed(eye.x,eye.y,eye.t,3);
[eye.sacloc, eye.sac_thresh, eye.peakout, eye.thresh_data] = EL1k_sacdetect(eye.t, eye.speed);%%#ok<NASGU> %, MULT, minthresh, peakthresh)
for ii=1:size(eye.sacloc,1)% get distance traveled
    eye.sac_dist(ii,:)=sqrt(abs(diff(eye.x(eye.sacloc(ii,:))))^2+...
        abs(diff(eye.y(eye.sacloc(ii,:))))^2);
end
fprintf('blink & saccade took: %4.2f seconds\n',toc)
E.eye=eye;

clear SEND_SCREEN_METRICS

%% load mat file
load([fname(1:end-3) 'mat'])

%% create B, clear others
B=A;
if B.change_state_time(1,2)>B.change_state_time(2,2)
    B.change_state_time(1,2)=0; % bug fix
end
B.change_state_time(:,2)=round(B.change_state_time(:,2)/int)*int; % force to E time resolution
temp=ones(size(B.change_state_time(:,2)));
st= B.change_state_time(B.change_state_time(:,2)>0,2);
%avoids 5 in-a-row of same times (otherwise have to go through times 1 by 1)
for ii =1:5
    [time, ind1{ii}]=intersect(eye.t,st(ii:5:end)); %#ok<AGROW,ASGLU>
end

ind=sort(vertcat(ind1{:}));
%eye.t(ind)
if length(ind)<length(st)
    error('length(ind)<length(st): not ok! length(ind)-length(st) = %d',length(ind)-length(st))
end
temp(B.change_state_time(:,2)>0)=ind; % to find edf timestamp of eyetrace easier
B.change_state_time(:,3)=temp;clear temp

% things i want from E
fields={'recdate','mesindex','mestimes','messages','t_adjust','res','eye'};
for ii=1:length(fields)
    B.(fields{ii})=E.(fields{ii});
end% for ii=1:length(Enames)
%% save
new_name=fname;new_name(end-3)=[];
save([new_name 'B.mat'],'B');
return
